package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.customer.CustomerQueryDTO;
import com.qk.entity.Customer;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerQueryVO;
import com.qk.vo.customer.CustomerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 *
 * @author lyl
 * @created 2025/10/18 21:28
 * @description
 *
 */
@Service
@SuppressWarnings("all")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<CustomerQueryVO> getListByPage(CustomerQueryDTO queryDTO) {
        Page<CustomerQueryVO> page = customerMapper.getListByPage(Page.of(queryDTO.getPage(),queryDTO.getPageSize()),queryDTO);
        return PageResult.<CustomerQueryVO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getRecords())
                .build();
    }

    @Override
    public void addCustomer(Customer customer) {
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }

    @Override
    public CustomerVO getCustomerById(Integer id) {
        Customer customer = this.baseMapper.selectById(id);
        return BeanUtil.toBean(customer, CustomerVO.class);
    }

    @Override
    public void updateCustomer(Customer customer) {
        boolean hasNullField = BeanUtil.hasNullField(customer, "channel", "gender", "age", "wechat", "qq", "degree", "jobStatus", "subject", "courseId", "businessId", "createTime", "updateTime");
        if (hasNullField){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(customer);
    }
}
